package com.example.rentalcar.util;

import com.example.rentalcar.entity.OssProperties;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Map;

//通过键值对获取阿里云oss配置文件数据
@Component
public class OssUtil {

    public OssProperties getOssProperties(){
        Yaml yaml = new Yaml();
        OssProperties ossProperties;
        URL url = LiquibaseDataSource.class.getClassLoader().getResource("aliyunOSS.yml");
        if (url != null) {
            try {
                Map map = (Map)((Map)((Map)yaml.load(new FileInputStream(url.getFile()))).get("aliyun")).get("oss");
                ossProperties = new OssProperties((String)map.get("endpoint"), (String)map.get("key-id"), (String)map.get("key-secret"), (String)map.get("region-id"), (String)map.get("bucket-name"), (String)map.get("path"));
                return ossProperties;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}